﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System;
using System.ComponentModel;
using System.Drawing.Design;
using NetJsWire.Lite.Converters;
using NetJsWire.Lite.Editors;
using NetJsWire.Lite.Widgets.UI.Util;
using Newtonsoft.Json;

namespace NetJsWire.Lite.Widgets.UI.Grid
{
    [JsonObject( MemberSerialization = MemberSerialization.OptOut )]
    [DesignTimeVisible( false )]
    [Serializable]
    [ToolboxItem( false )]
    public class ColumnModel : BaseObservable
    {
        [NonSerialized]
        private readonly GridPanel _gridOwner;

        private ColumnCollection _columns;// = new List<Column>();

        private Column _defaults;

        public ColumnModel( GridPanel owner )
        {
            if ( owner == null )
            {
                throw new NullReferenceException( "The owner cannot be null" );
            }
            _gridOwner = owner;
            _columns = new ColumnCollection( this );
            _defaults = new Column( this );
        }

        [Browsable( true )]
        [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
        [JsonProperty( "defaults", DefaultValueHandling = DefaultValueHandling.Ignore )]
        [TypeConverter( typeof( ExpandableObjectConverter ) )]
        public Column Defaults
        {
            get { return _defaults; }
            set { _defaults = value; }
        }

        [Browsable( true )]
        [DesignerSerializationVisibility( DesignerSerializationVisibility.Content )]
        [JsonProperty( "columns", Required = Required.Always )]
        [Editor( typeof( ColumnCollectionEditor ), typeof( UITypeEditor ) )]
        public ColumnCollection Columns
        {
            get { return _columns; }
            set
            {
                _columns = value;
                if ( _gridOwner != null )
                    _gridOwner.InvalidateWindow();
            }
        }

        [DefaultValue( false )]
        [JsonProperty( "defaultSortable", DefaultValueHandling = DefaultValueHandling.Ignore )]
        public bool DefaultSortable { get; set; }

        [DefaultValue( ( string ) null )]
        [JsonConverter( typeof( NotQuotedJsonStringConverter ) )]
        [JsonProperty( "isCellEditable" )]
        [Editor( typeof( ScriptEditor ), typeof( UITypeEditor ) )]
        public string IsCellEditable { get; set; }


        public bool ShouldSerializeColumns()
        {
            return Columns != null && Columns.Count > 0;
        }

        public bool ShouldSerializeDefaults()
        {
            return Defaults != null && ( !string.IsNullOrWhiteSpace( Defaults.Align ) || !string.IsNullOrWhiteSpace( Defaults.Css ) || !Defaults.Editable || !string.IsNullOrWhiteSpace( Defaults.Editor ) || Defaults.MenuDisabled );
        }

        internal void NotifyParentToRedraw()
        {
            if ( _gridOwner != null )
                _gridOwner.InvalidateWindow();
        }
    }
}
